<?php
final class DeviceToCircleModel {
	private $mysql;
	private $tableName = 'DeviceToCircle';

	public $idRowName = 'DeviceToCircle_ID';
	public $circleIDRowName = 'Circle_ID';
	public $deviceIDRowName = 'Device_ID';
	public $startDateTimeRowName = 'DeviceToCircle_StartDateTime';
	public $endDateTimeRowName = 'DeviceToCircle_EndDateTime';
	public $CountRowName = 'DeviceToCircle_Count';

	public function __construct ($mysql) {
		$this->mysql = $mysql;
	}

	public function GetDevicesByCircle ($CircleID) {
		$ref = [];
		$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`, `%s`, `%s`, `%s` FROM `%s` WHERE `%s` <= NOW() AND `%s` >= NOW() AND `%s`=\'%s\';', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->circleIDRowName), $this->mysql->escape_string ($this->deviceIDRowName), $this->mysql->escape_string ($this->startDateTimeRowName), $this->mysql->escape_string ($this->endDateTimeRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->startDateTimeRowName), $this->mysql->escape_string ($this->endDateTimeRowName), $this->mysql->escape_string ($this->circleIDRowName), $this->mysql->escape_string ($CircleID)));
//printf ('SELECT `%s`, `%s`, `%s`, `%s`, `%s` FROM `%s` WHERE `%s` <= NOW() AND `%s` >= NOW() AND `%s`=\'%s\';', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->circleIDRowName), $this->mysql->escape_string ($this->deviceIDRowName), $this->mysql->escape_string ($this->startDateTimeRowName), $this->mysql->escape_string ($this->endDateTimeRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->startDateTimeRowName), $this->mysql->escape_string ($this->endDateTimeRowName), $this->mysql->escape_string ($this->circleIDRowName), $this->mysql->escape_string ($CircleID));
		$len = $sqlresult->affected_rows ();
#		if ($len > 0) {
#			$ref = $sqlresult->fetch_array ();
#		}
		while ($data = $sqlresult->fetch_array ()) {
			$ref[] = $data;
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function GetDevicesByCircleCount ($CircleID) {
//printf ('SELECT COUNT(*) AS `%s` FROM `%s` WHERE `%s` <= NOW() AND `%s` >= NOW() AND `%s`=\'%s\';', $this->mysql->escape_string ($this->CountRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->startDateTimeRowName), $this->mysql->escape_string ($this->endDateTimeRowName), $this->mysql->escape_string ($this->circleIDRowName), $this->mysql->escape_string ($CircleID));
		$sqlresult = $this->mysql->query (sprintf ('SELECT COUNT(*) AS `%s` FROM `%s` WHERE `%s` <= NOW() AND `%s` >= NOW() AND `%s`=\'%s\';', $this->mysql->escape_string ($this->CountRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->startDateTimeRowName), $this->mysql->escape_string ($this->endDateTimeRowName), $this->mysql->escape_string ($this->circleIDRowName), $this->mysql->escape_string ($CircleID)));
#		$len = $sqlresult->affected_rows ();
		$ref = $sqlresult->fetch_array ();
		$len = $ref[$this->CountRowName];
		$sqlresult->free_result ();
//print $len;
		return $len;
	}

	public function GetCirclesByDevice ($DeviceID) {
		$ref = [];
		$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`, `%s`, `%s`, `%s` FROM `%s` WHERE `%s` <= NOW() AND `%s` >= NOW() AND `%s`=\'%s\';', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->circleIDRowName), $this->mysql->escape_string ($this->deviceIDRowName), $this->mysql->escape_string ($this->startDateTimeRowName), $this->mysql->escape_string ($this->endDateTimeRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->startDateTimeRowName), $this->mysql->escape_string ($this->endDateTimeRowName), $this->mysql->escape_string ($this->deviceIDRowName), $this->mysql->escape_string ($DeviceID)));
//printf ('SELECT `%s`, `%s`, `%s`, `%s`, `%s` FROM `%s` WHERE `%s` <= NOW() AND `%s` >= NOW() AND `%s`=\'%s\';', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->circleIDRowName), $this->mysql->escape_string ($this->deviceIDRowName), $this->mysql->escape_string ($this->startDateTimeRowName), $this->mysql->escape_string ($this->endDateTimeRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->startDateTimeRowName), $this->mysql->escape_string ($this->endDateTimeRowName), $this->mysql->escape_string ($this->deviceIDRowName), $this->mysql->escape_string ($DeviceID));
		$len = $sqlresult->affected_rows ();
#		if ($len > 0) {
#			$ref = $sqlresult->fetch_array ();
#		}
		while ($data = $sqlresult->fetch_array ()) {
			$ref[] = $data;
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function GetCirclesByDeviceCount ($DeviceID) {
		$sqlresult = $this->mysql->query (sprintf ('SELECT COUNT(*) AS `%s` FROM `%s` WHERE `%s` <= NOW() AND `%s` >= NOW() AND `%s`=\'%s\';', $this->mysql->escape_string ($this->CountRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->startDateTimeRowName), $this->mysql->escape_string ($this->endDateTimeRowName), $this->mysql->escape_string ($this->deviceIDRowName), $this->mysql->escape_string ($DeviceID)));
#		$len = $sqlresult->affected_rows ();
		$ref = $sqlresult->fetch_array ();
		$len = $ref[$this->CountRowName];
		$sqlresult->free_result ();
		return $len;
	}


	public function DelDeviceToCircle ($id) {
		$len = 0;
		$sqlresult = $this->mysql->query (sprintf ('DELETE FROM `%s` WHERE `%s`=\'%s\';', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($id)));
		$len = $sqlresult->affected_rows ();
		$sqlresult->free_result ();
		return $len;
	}

	public function CheckCircleAvailable ($CircleID) {
/*		$sqlresult = $this->mysql->query (sprintf ('SELECT COUNT(*) AS `%s` FROM `%s` WHERE `%s`=%d AND `%s` < NOW() AND `%s` > NOW();', $this->mysql->escape_string ($this->CountRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->circleIDRowName), $this->mysql->escape_string ($CircleID), $this->mysql->escape_string ($this->startDateTimeRowName), $this->mysql->escape_string ($this->endDateTimeRowName)));
		$ref = $sqlresult->fetch_array ();
		$len = $ref[$this->CountRowName];
		$sqlresult->free_result ();
		return $len;*/
		return $this->GetDevicesByCircleCount ($CircleID);
	}


	public function AddDeviceToCircle ($did, $cid, $startDate, $endDate) {
		$sqlresult = $this->mysql->query (sprintf ('INSERT INTO `%s` (`%s`, `%s`, `%s`, `%s`) VALUES (%d, %d, \'%s\', \'%s\');', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->deviceIDRowName), $this->mysql->escape_string ($this->circleIDRowName), $this->mysql->escape_string ($this->startDateTimeRowName), $this->mysql->escape_string ($this->endDateTimeRowName), $this->mysql->escape_string ($did), $this->mysql->escape_string ($cid), $this->mysql->escape_string ($startDate), $this->mysql->escape_string ($endDate)));
		$sqlresult->free_result ();
	}

	public function UpdateDeviceToCircle ($id, $did, $cid, $startDate, $endDate) {
		$sqlresult = $this->mysql->query (sprintf ('UPDATE `%s` SET `%s`=%d, `%s`=%d, `%s`=\'%s\', `%s`=\'%s\' WHERE `%s`=%d;', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->deviceIDRowName), $this->mysql->escape_string ($did), $this->mysql->escape_string ($this->circleIDRowName), $this->mysql->escape_string ($cid), $this->mysql->escape_string ($this->startDateTimeRowName), $this->mysql->escape_string ($startDate), $this->mysql->escape_string ($this->endDateTimeRowName), $this->mysql->escape_string ($endDate), $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($id)));
		$sqlresult->free_result ();
	}

	public function CleanUPCircle () {
		$sqlresult = $this->mysql->query (sprintf ('DELETE FROM `%s` WHERE `%s` < NOW();', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->endDateTimeRowName)));
		$sqlresult->free_result ();
	}

/*
	public function GetDeviceCount () {
		$ref = 0;
		$sqlresult = $this->mysql->query (sprintf ('SELECT COUNT(*) AS `%s` FROM `%s`;', $this->mysql->escape_string ($this->CountRowName), $this->mysql->escape_string ($this->tableName)));
		$len = $sqlresult->num_rows ();
		if ($len > 0) {
			$data = $sqlresult->fetch_array ();
			$ref = $data[$this->CountRowName];
		}
		$sqlresult->free_result ();
		return $ref;
	}


	public function CheckDeviceItemExist ($IMEI) {
		$ref = NULL;
		$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s` FROM `%s` WHERE `%s`=\'%s\';', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->imeiRowName), $this->mysql->escape_string ($this->phoneNumberRowName), $this->mysql->escape_string ($this->simSerialNumberRowName), $this->mysql->escape_string ($this->imsiRowName), $this->mysql->escape_string ($this->manufacturerRowName), $this->mysql->escape_string ($this->modelRowName), $this->mysql->escape_string ($this->osRowName), $this->mysql->escape_string ($this->versionRowName), $this->mysql->escape_string ($this->remarkRowName),  $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->imeiRowName), $this->mysql->escape_string ($IMEI)));
		$len = $sqlresult->affected_rows ();
		if ($len > 0) {
			$ref = $sqlresult->fetch_array ();
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function GetDeviceItems ($offset, $count) {
		$ref = array ();
		$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`,  `%s`,  `%s`,  `%s`, `%s`,  `%s`,  `%s`,  `%s`,  `%s` FROM `%s` LIMIT %d, %d;', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->imeiRowName), $this->mysql->escape_string ($this->phoneNumberRowName), $this->mysql->escape_string ($this->simSerialNumberRowName), $this->mysql->escape_string ($this->imsiRowName), $this->mysql->escape_string ($this->manufacturerRowName), $this->mysql->escape_string ($this->modelRowName), $this->mysql->escape_string ($this->osRowName), $this->mysql->escape_string ($this->versionRowName), $this->mysql->escape_string ($this->remarkRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($offset), $this->mysql->escape_string ($count)));
		while ($data = $sqlresult->fetch_array ()) {
			$ref[] = $data;
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function DelDeviceItem ($id) {
		$len = 0;
		$sqlresult = $this->mysql->query (sprintf ('DELETE FROM `%s` WHERE `%s`=\'%s\';', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($id)));
		$len = $sqlresult->affected_rows ();
		$sqlresult->free_result ();
		return $len;
	}


	public function AddDeviceItem ($IMEI, $PhoneNumber, $SimSerialNumber, $IMSI, $Manufacturer, $Model, $OS, $Version, $Remark = '') {
		$sqlresult = $this->mysql->query (sprintf ('INSERT INTO `%s` (`%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`) VALUES (\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\');', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->imeiRowName), $this->mysql->escape_string ($this->phoneNumberRowName), $this->mysql->escape_string ($this->simSerialNumberRowName), $this->mysql->escape_string ($this->imsiRowName), $this->mysql->escape_string ($this->manufacturerRowName), $this->mysql->escape_string ($this->modelRowName), $this->mysql->escape_string ($this->osRowName), $this->mysql->escape_string ($this->versionRowName), $this->mysql->escape_string ($this->remarkRowName), $this->mysql->escape_string ($IMEI), $this->mysql->escape_string ($PhoneNumber), $this->mysql->escape_string ($SimSerialNumber), $this->mysql->escape_string ($IMSI), $this->mysql->escape_string ($Manufacturer), $this->mysql->escape_string ($Model), $this->mysql->escape_string ($OS), $this->mysql->escape_string ($Version), $this->mysql->escape_string ($Remark)));
		$sqlresult->free_result ();
		return $this->mysql->insert_id ();
	}
*/
}
?>
